package com.gzhryc.shared_device.oem.h5.mng.controllers.cdcz;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.cdcz.code.dao.db.ChargeSocketSlaveDevice;
import com.gzhryc.shared_device.cdcz.code.dao.enums.ChargeSocketSlaveDeviceEnum;
import com.gzhryc.shared_device.cdcz.code.services.ChargeSocketSlaveDeviceService;
import com.gzhryc.shared_device.cdcz.code.services.dto.ChargeSocketSlaveDeviceSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

public class ChargeSocketSlaveDeviceController {

    static Logger log = Logger.getLogger(ChargeSocketSlaveDeviceController.class);

    @Route("/cdcz/slave_device/list.html")
    public String list(HttpServletRequest request) {
        request.setAttribute("stateList", ChargeSocketSlaveDeviceEnum.State.values());
        return "/cdcz/slave_device/list.html";
    }

    @Route(value = "/cdcz/slave_device/list.data")
    public ListResult<ChargeSocketSlaveDevice> listData(HttpServletRequest request) {
        ListResult<ChargeSocketSlaveDevice> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");
        ChargeSocketSlaveDeviceSearch search = WebUtils.getParameter(request, ChargeSocketSlaveDeviceSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(ChargeSocketSlaveDeviceService.self().countBySearch(search));
        List<ChargeSocketSlaveDevice> deviceList = ChargeSocketSlaveDeviceService.self().findBySearch(search, pageNo, pageLimit);
        if(deviceList != null && deviceList.size() > 0){
            result.setCode(DtoConstants.SUCCESS, null);
            result.getBody().setData(deviceList);
        }
        return result;
    }

    @Route(value = { "/cdcz/slave_device/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除充电插座从机失败");
        String deviceSn = WebUtils.getString(request,"deviceSn");
        if(StringTools.isNotBlank(deviceSn)) {
            try {
                if (ChargeSocketSlaveDeviceService.self().deleteByDeviceSn(deviceSn)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

}
